#include <stdio.h>
#include <string.h>
#include "mpi.h"

void Get_data2(int my_rank, float* a_ptr, float* b_ptr, int* n_ptr);


int main (int argc, char** argv) 
{
    int my_rank; /* Rank of process */
    int p; /* Number of processes */
    int source; /* Rank of sender */
    int dest; /* Rank of receiver */
    int tag = 50; /* Tag for messages */
    char message[100]; /* Storage for the message */
    MPI_Status status; /* Return status for receive */
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
    MPI_Comm_size(MPI_COMM_WORLD, &p);
    if (my_rank != 0) 
    {
	sprintf(message, "Greetings from process %d!", my_rank);
	dest = 0;
	/* Use strlen(message)+1 to include '\0' */
	MPI_Send(message, strlen(message)+1, MPI_CHAR, dest,
		 tag, MPI_COMM_WORLD);
    } 
    else 
    { /* my_rank == 0 */
	for (source = 1; source < p; source++) 
	{
	    MPI_Recv(message, 100, MPI_CHAR, source, MPI_ANY_TAG,
		     MPI_COMM_WORLD, &status);
	    printf("%s\n", message);
	}
    }
    float a;
    float b;
    int n;
    Get_data2(my_rank, &a, &b, &n);
    printf("I'm process %d, here a = %f, b = %f, n = %d\n", my_rank, a, b, n);

    float sum;
    float num = (float)my_rank;
    MPI_Reduce(&num, &sum, 1, MPI_FLOAT, MPI_SUM, 0, MPI_COMM_WORLD);
    printf("I'm process %d, here sum = %f, num = %f\n", my_rank, sum, num);
    
    MPI_Finalize();
    return 0;
} /* main */

void Get_data2(int my_rank, float* a_ptr, float* b_ptr, int* n_ptr) 
{
    int root = 0; /* Arguments to MPI_Bcast */
    int count = 1;
    if (my_rank == 0)
    {
	printf("Enter a, b, and n\n");
	scanf("%f %f %d", a_ptr, b_ptr, n_ptr);
    }
    MPI_Bcast(a_ptr, 1, MPI_FLOAT, root,
	      MPI_COMM_WORLD);
    MPI_Bcast(b_ptr, 1, MPI_FLOAT, root,
	      MPI_COMM_WORLD);
    MPI_Bcast(n_ptr, 1, MPI_INT, root,
	      MPI_COMM_WORLD);
} /* Get_data2 */
